/*
 * Copyright (C) 2025 Tsingteng MicroSystem
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * @file nci_defs.h
 * @brief 定义NCI相关的结构和常量
 */

#ifndef _NCI_DEFS_H_
#define _NCI_DEFS_H_

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#define NCI_HDR_SIZE 3
#define NCI_MT_MASK 0x07
#define NCI_MT_SHIFT 5
#define NCI_GID_MASK 0x0F
#define NCI_OID_MASK 0x3F

#define NCI_HDR0(_mt, _gid) ((((_mt) << NCI_MT_SHIFT) | ((_gid) & NCI_GID_MASK)) & 0xFF)
#define NCI_HDR1(_oid) ((_oid) & NCI_OID_MASK)

/** @brief NCI状态枚举 */
typedef enum {
    NCI_STATUS_OK = 0x00,
    NCI_STATUS_REJECTED = 0x01,
    NCI_STATUS_FAILED = 0x03,
    NCI_STATUS_NOT_INITIALIZED = 0x04,
    NCI_STATUS_SYNTAX_ERROR = 0x05,
    NCI_STATUS_SEMANTIC_ERROR = 0x06,

    NCI_STATUS_INVALID_PARAM = 0x09,
    NCI_STATUS_MESSAGE_SIZE_EXCEEDED = 0x0A,

    NCI_STATUS_OK_1_BIT = 0x11,
    NCI_STATUS_OK_2_BIT = 0x12,
    NCI_STATUS_OK_3_BIT = 0x13,
    NCI_STATUS_OK_4_BIT = 0x14,
    NCI_STATUS_OK_5_BIT = 0x15,
    NCI_STATUS_OK_6_BIT = 0x16,
    NCI_STATUS_OK_7_BIT = 0x17,

    NCI_STATUS_DISCOVERY_ALREADY_STARTED = 0xA0,
    NCI_STATUS_DISCOVERY_TARGET_ACTIVATION_FAILED = 0xA1,
    NCI_STATUS_DISCOVERY_TEAR_DOWN = 0xA2,

    NCI_STATUS_RF_FRAME_CORRUPTED = 0x02,
    NCI_STATUS_RF_TRANSMISSION_EXCEPTION = 0xB0,
    NCI_STATUS_RF_PROTOCOL_EXCEPTION = 0xB1,
    NCI_STATUS_RF_TIMEOUT_EXCEPTION = 0xB2,
    NCI_STATUS_RF_UNEXPECTED_DATA = 0xB3,

    NCI_STATUS_NFCEE_INTERFACE_ACTIVATION_FAILED = 0xC0,
    NCI_STATUS_NFCEE_TRANSMISSION_ERROR = 0xC1,
    NCI_STATUS_NFCEE_PROTOCOL_ERROR = 0xC2,
    NCI_STATUS_TIMEOUT_ERROR = 0xC3,

    NCI_STATUS_CE_SET_LISTEN_PARAMS_FAILED = 0xD0,
    NCI_STATUS_CE_LIST_NODE_FAILED = 0xD1,
    NCI_STATUS_CE_GET_ACTS_FAILED = 0xD2,

    NCI_STATUS_EXT_FAILED = 0x101,
    NCI_STATUS_EXT_NOT_INITIALIZED,
    NCI_STATUS_EXT_INVALID_PARAM,
    NCI_STATUS_EXT_REJECTED,
    NCI_STATUS_EXT_TP_ERROR,
    NCI_STATUS_EXT_CMD_SEND_FAILED,
    NCI_STATUS_EXT_WAIT_RSP_TIMEOUT,
    NCI_STATUS_EXT_WAIT_NTF_TIMEOUT,
    NCI_STATUS_EXT_CALLBACK_FULL,
    NCI_STATUS_EXT_NOT_FOUND,
    NCI_STATUS_EXT_ALLOC_FAILED,
    NCI_STATUS_EXT_INIT_LOCK_FAILED,
    NCI_STATUS_EXT_GET_LOCK_FAILED,
    NCI_STATUS_EXT_WAIT_FAILED,
    NCI_STATUS_EXT_COPY_FAILED,
    NCI_STATUS_EXT_TIMEOUT,
    NCI_STATUS_EXT_CHANNEL_NOT_AVAILABLE,
    NCI_STATUS_EXT_STATE_INCORRECT,
    NCI_STATUS_EXT_QUEUE_ERROR,
    NCI_STATUS_EXT_NOT_FREE,
    NCI_STATUS_EXT_TAG_LOST,
    NCI_STATUS_EXT_NOT_SUPPORTED,
    NCI_STATUS_EXT_ALREADY_OPENED,
    NCI_STATUS_EXT_APDU_STATUS_ERROR,
    NCI_STATUS_EXT_EMPTY_DATA,
} tNCI_STATUS;

typedef enum {
    NCI_VERSION_1_0 = 0x10,
    NCI_VERSION_1_1 = 0x11,
    NCI_VERSION_2_0 = 0x20,
} tNCI_VERSION;

typedef uint8_t tNCI_RF_DISCOVERY_ID;

typedef enum {
    NCI_RF_TECH_A = 0x00,
    NCI_RF_TECH_B = 0x01,
    NCI_RF_TECH_F = 0x02,
    NCI_RF_TECH_V = 0x03,
} tNCI_RF_TECH;

typedef uint8_t tNCI_RF_PROTOCOL;
#define NCI_RF_PROTOCOL_UNDETERMINED 0x00
#define NCI_RF_PROTOCOL_T1T 0x01
#define NCI_RF_PROTOCOL_T2T 0x02
#define NCI_RF_PROTOCOL_T3T 0x03
#define NCI_RF_PROTOCOL_ISO_DEP 0x04
#define NCI_RF_PROTOCOL_NFC_DEP 0x05
#define NCI_RF_PROTOCOL_T5T 0x06
#define NCI_RF_PROTOCOL_NDEF 0x07
#define NCI_RF_PROTOCOL_KOVIO 0x70
#define NCI_RF_PROTOCOL_M1 0x80
#define NCI_RF_PROTOCOL_ITAP 0x66

typedef uint8_t tNCI_RF_INTERFACE;
#define NCI_RF_INTERFACE_NFCEE_DIRECT 0x00
#define NCI_RF_INTERFACE_FRAME 0x01
#define NCI_RF_INTERFACE_ISO_DEP 0x02
#define NCI_RF_INTERFACE_NFC_DEP 0x03
#define NCI_RF_INTERFACE_NDEF 0x06
#define NCI_RF_INTERFACE_TAG_CMD 0x80
#define NCI_RF_INTERFACE_ITAP 0x66

typedef enum {
    NCI_ROUTING_ENTRY_TECH = 0x0,
    NCI_ROUTING_ENTRY_PROTOCOL = 0x1,
    NCI_ROUTING_ENTRY_AID = 0x2,
    NCI_ROUTING_ENTRY_SYSTEMCODE = 0x3,
    NCI_ROUTING_ENTRY_APDU_PATTERN = 0x4,
} tNCI_ROUTING_ENTRY_TYPE;

#define NCI_ROUTING_QUALIFIER_BLOCKED 0x40
#define NCI_ROUTING_QUALIFIER_AID_SUFFIX 0x20
#define NCI_ROUTING_QUALIFIER_AID_PREFIX 0x10

#define NCI_POWER_STATE_SWITCHED_ON 0x01
#define NCI_POWER_STATE_SWITCHED_OFF 0x02
#define NCI_POWER_STATE_BATTERY_OFF 0x04
#define NCI_POWER_SUB_STATE_SCREEN_OFF_UNLOCKED 0x08
#define NCI_POWER_SUB_STATE_SCREEN_ON_LOCKED 0x10
#define NCI_POWER_SUB_STATE_SCREEN_OFF_LOCKED 0x20

#define NCI_SCREEN_STATE_UNKNOWN      0x00
#define NCI_SCREEN_STATE_OFF_UNLOCKED 0x01
#define NCI_SCREEN_STATE_OFF_LOCKED   0x02
#define NCI_SCREEN_STATE_ON_LOCKED    0x04
#define NCI_SCREEN_STATE_ON_UNLOCKED  0x08

#define NCI_SCREEN_POLLING_TAG_MASK   0x10

#define NCI_MAX_NFCID1_LEN 10
#define NCI_T1T_HR_LEN 2
#define NCI_SENS_RES_LEN 2

typedef struct {
    uint8_t sens_res[NCI_SENS_RES_LEN];
    uint8_t nfcid1_len;
    uint8_t nfcid1[NCI_MAX_NFCID1_LEN];
    uint8_t sel_res;
    uint8_t hr_length;
    uint8_t hr[NCI_T1T_HR_LEN];
} tNCI_RF_TECH_PARAM_PA;

#define NCI_MAX_SENSB_RES_LEN 12

typedef struct {
    uint8_t sensb_res_len;
    uint8_t sensb_res[NCI_MAX_SENSB_RES_LEN];
} tNCI_RF_TECH_PARAM_PB;

typedef struct {
    uint8_t sensb_req;
} tNCI_RF_TECH_PARAM_LB;

#define NCI_MAX_SENSF_RES_LEN 18

typedef struct {
    uint8_t bit_rate;
    uint8_t sensf_res_len;
    uint8_t sensf_res[NCI_MAX_SENSF_RES_LEN];
} tNCI_RF_TECH_PARAM_PF;

#define NCI_NFCID2_LEN 8
#define NCI_PMM_LEN 8
#define NCI_SYSTEM_CODE_LEN 2

typedef struct {
    uint8_t nfcid2[NCI_NFCID2_LEN];
} tNCI_RF_TECH_PARAM_LF;

#define NCI_TECH_PARAM_PV_UID_LEN 8

typedef struct {
    uint8_t res_flag;
    uint8_t dsfid;
    uint8_t uid[NCI_TECH_PARAM_PV_UID_LEN];
} tNCI_RF_TECH_PARAM_PV;

typedef union {
    tNCI_RF_TECH_PARAM_PA pa;
    tNCI_RF_TECH_PARAM_PB pb;
    tNCI_RF_TECH_PARAM_PF pf;
    tNCI_RF_TECH_PARAM_PV pv;
    tNCI_RF_TECH_PARAM_LF lf;
    tNCI_RF_TECH_PARAM_LB lb;
} tNCI_RF_TECH_PARAMS;

#define NCI_MAX_RATS_LEN 64

typedef struct {
    uint8_t rats_response_len;
    uint8_t rats_response[NCI_MAX_RATS_LEN];
    uint8_t his_bytes_len;
    uint8_t *his_bytes;
} tNCI_RF_ACT_PARAM_PA_ISO_DEP;

typedef struct {
    uint8_t rats_command;
} tNCI_RF_ACT_PARAM_LA_ISO_DEP;

#define NCI_MAX_ATTRIB_LEN 58

typedef struct {
    uint8_t attrib_res_len;
    uint8_t attrib_res[NCI_MAX_ATTRIB_LEN];
    uint8_t hi_layer_len;
    uint8_t *hi_layer_bytes;
} tNCI_RF_ACT_PARAM_PB_ISO_DEP;

typedef struct {
    uint8_t attrib_req_len;
    uint8_t attrib_req[NCI_MAX_ATTRIB_LEN];
} tNCI_RF_ACT_PARAM_LB_ISO_DEP;

typedef union {
    tNCI_RF_ACT_PARAM_PA_ISO_DEP pa_iso_dep;
    tNCI_RF_ACT_PARAM_LA_ISO_DEP la_iso_dep;
    tNCI_RF_ACT_PARAM_PB_ISO_DEP pb_iso_dep;
    tNCI_RF_ACT_PARAM_LB_ISO_DEP lb_iso_dep;
} tNCI_RF_ACT_PARAMS;

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif // _NCI_DEFS_H_
